﻿using Microsoft.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Workplatm;
using Workplatm.model;
using WorkPlatm_CM_EBank2RJZ.model;
using WorkPlatm_CM_EBank2RJZ.views.dialogs;
using wxt;

namespace WorkPlatm_CM_EBank2RJZ.views.pages
{
    /// <summary>
    /// Interaction logic for Page_CM_EBank2RJZ.xaml
    /// </summary>
    public partial class Page_CM_EBank2RJZ : Page
    {
        //保存所有单据信息list
        List<Ebk_DJXX> list_djxx_all = new List<Ebk_DJXX>();
        //保存未支付单据信息list
        List<Ebk_DJXX> list_djxx_wzf = new List<Ebk_DJXX>();
        //保存已经支付单据信息list
        List<Ebk_DJXX> list_djxx_yzf = new List<Ebk_DJXX>();
        //保存已经支付生成单据信息list
        List<Ebk_DJXX> list_djxx_ysc = new List<Ebk_DJXX>();
        public Page_CM_EBank2RJZ()
        {
            InitializeComponent();
            try
            {
                String resultInfo;
                //获取起始日期
                this.com_dateStart.D_DateVal = LoginInfo.getInstance().Dywrq;
                this.com_dateEnd.D_DateVal = LoginInfo.getInstance().Dywrq.AddDays(7);

                //1、获取银行账号
                String sql_yhzh = "select yhzh,yhzhmc from PUBSZDWZH where gsdm = '" + LoginInfo.getInstance().Hsdwdm + "' and kjnd = '" + LoginInfo.getInstance().Dywrq.Year + "' and wyzf = '1' ";
                DataSet ds_yhzh = Dao.getSql(sql_yhzh, "PUBSZDWZH", out resultInfo);
                if (ds_yhzh != null && ds_yhzh.Tables.Count > 0 && ds_yhzh.Tables[0].Rows.Count > 0)
                {
                    this.comb_yhzh.Items.Clear();
                    ComboBoxItem cbi_all = new ComboBoxItem();
                    cbi_all.Name = "_";
                    cbi_all.Content = "所有账户";
                    this.comb_yhzh.Items.Add(cbi_all);
                    foreach (DataRow row in ds_yhzh.Tables[0].Rows)
                    {
                        String yhzhmc = row["yhzhmc"].ToString();
                        String yhzh = row["yhzh"].ToString();
                        ComboBoxItem cbi = new ComboBoxItem();
                        cbi.Name = "_" + yhzh;
                        cbi.Content = yhzh + "-" + yhzhmc;
                        this.comb_yhzh.Items.Add(cbi);
                    }
                    this.comb_yhzh.SelectedItem = cbi_all;
                }
                //获取支付数据
                iniData();
            }
            catch (Exception e1)
            {
                File.AppendAllText("c:\\log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n" + e1.Message);
            }
        }
        //初始化信息
        private void iniData()
        {
            this.dg_yhdjxx_all.ItemsSource = null;
            this.dg_yhdjxx_wzf.ItemsSource = null;
            this.dg_yhdjxx_yzf.ItemsSource = null;
            this.dg_yhdjxx_ysc.ItemsSource = null;
            //清空集合 
            this.list_djxx_all.Clear();
            this.list_djxx_wzf.Clear();
            this.list_djxx_yzf.Clear();
            this.list_djxx_ysc.Clear();

            String resultInfo = "";

            //获取付款行银行账号
            String fyhzh = (this.comb_yhzh.SelectedItem as ComboBoxItem).Name.Equals("_") ? "" : (this.comb_yhzh.SelectedItem as ComboBoxItem).Name.Substring(1);
            //2、获取单据已经支付信息
            String sql_djxx = "exec ebksp_wxt_getDjxx '" + LoginInfo.getInstance().Hsdwdm + "','" + LoginInfo.getInstance().Dywrq.Year + "','" + fyhzh + "',''";
            DataSet ds_djxx = Dao.getSql(sql_djxx, "ebksp_wxt_getDjxx", out resultInfo);
            if (ds_djxx != null && ds_djxx.Tables.Count > 0 && ds_djxx.Tables[0].Rows.Count > 0 && ds_djxx.Tables[0].Rows[0]["je"]!= null && ds_djxx.Tables[0].Rows[0]["je"] != DBNull.Value && !ds_djxx.Tables[0].Rows[0]["je"].ToString().Trim().Equals(""))
            {
                foreach (DataRow row in ds_djxx.Tables[0].Rows)
                {
                    int djzt = int.Parse(row["zt"].ToString().Trim());
                    Ebk_DJXX dj = new Ebk_DJXX();
                    Ebk_DJXX dj_all = new Ebk_DJXX();
                    dj_all.Zfid = row["zfid"].ToString();
                    dj.Zfid = row["zfid"].ToString();
                    dj_all.Djly = row["djly"].ToString();
                    dj.Djly = row["djly"].ToString();
                    dj_all.Djrq = row["djrq"].ToString();
                    dj.Djrq = row["djrq"].ToString();
                    dj_all.Fyhzh = row["fyhzh"].ToString();
                    dj.Fyhzh = row["fyhzh"].ToString();
                    dj_all.Je = Decimal.Parse(row["je"].ToString().Replace(",", ""));
                    dj.Je = Decimal.Parse(row["je"].ToString().Replace(",", ""));
                    dj_all.Ywlxdm = row["ywlxdm"].ToString();
                    dj.Ywlxdm = row["ywlxdm"].ToString();
                    dj.Ywlxmc = row["ywlxmc"].ToString();
                    dj_all.Ywlxmc = row["ywlxmc"].ToString();
                    dj.Zfrq = row["zfrq"].ToString();
                    dj_all.Zfrq = row["zfrq"].ToString();
                    dj.Skr = row["skr"].ToString();
                    dj_all.Skr = row["Skr"].ToString();
                    dj.Syhzh = row["Syhzh"].ToString();
                    dj_all.Syhzh = row["Syhzh"].ToString();
                    dj.Zt = row["zt"].ToString();
                    dj_all.Zt = row["zt"].ToString();
                    dj.Zy = row["zy"].ToString();
                    dj_all.Zy = row["zy"].ToString();
                    dj.Slzt = row["slzt"].ToString();
                    dj_all.Slzt = row["slzt"].ToString();
                    dj.Rzzt = row["rzzt"].ToString();
                    dj_all.Rzzt = row["rzzt"].ToString();
                    if (djzt <= 0)
                    {
                        dj.Xh = this.list_djxx_wzf.Count + 1;
                        this.list_djxx_wzf.Add(dj);
                    }
                    else if (djzt == 1)
                    {
                        dj.Xh = this.list_djxx_yzf.Count + 1;
                        this.list_djxx_yzf.Add(dj);
                    }
                    else if (djzt == 2)
                    {
                        dj.Xh = this.list_djxx_ysc.Count + 1;
                        this.list_djxx_ysc.Add(dj);
                    }
                    dj_all.Xh = this.list_djxx_all.Count + 1;
                    this.list_djxx_all.Add(dj_all);
                }
            }
            this.dg_yhdjxx_all.ItemsSource = this.list_djxx_all;
            this.dg_yhdjxx_wzf.ItemsSource = this.list_djxx_wzf;
            this.dg_yhdjxx_yzf.ItemsSource = this.list_djxx_yzf;
            this.dg_yhdjxx_ysc.ItemsSource = this.list_djxx_ysc;
            //过滤信息
            filterData();
        }

        public void setParams(LoginInfo loginInfo)
        {

        }

        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            PageManager.getInstance().closePage(this.Name);
        }
        //生成按钮事件
        private void btn_Insert_Click(object sender, RoutedEventArgs e)
        {

            String result;
            if (this.dg_yhdjxx_yzf.SelectedItems.Count <= 0)
            {
                MessageBox.Show("未选中任何单据，请选择后重试！", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return;
            }
            else if (MessageBox.Show("当前选中"+this.dg_yhdjxx_yzf.SelectedItems.Count+"条记录，即将将选中的支付单生成日记账,是否继续？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question).Equals(MessageBoxResult.Yes))
            {
                String djh = "";
                foreach (Ebk_DJXX dj in this.dg_yhdjxx_yzf.SelectedItems)
                {
                    djh += ";" + dj.Zfid;
                    //1、首先检测该支付单据对应了响应的凭证没有，直接获取凭证号码或者是分录号。
                    String sql_pzh = "select idpzh,pzh,kjqj from gl_pzml pz where kjqj like  '" + LoginInfo.getInstance().Dywrq.Year + "'+'%' and gsdm = '" + LoginInfo.getInstance().Hsdwdm + "' and exists(select * from OER_DJML djml where  exists(select groupid from ebk_zfxx zfxx where zfxx.gsdm = '" + LoginInfo.getInstance().Hsdwdm + "' and zfxx.kjnd = '" + LoginInfo.getInstance().Dywrq.Year + "' and zfxx.zfid = '" + dj.Zfid + "' and djml.wyzfguid = zfxx.groupid ) and pz.idpzh = djml.IDPZH) ";
                    DataSet ds = Dao.getSql(sql_pzh, "gl_pzml", out result);
                    //2、如果存在凭证号则直接代入日记账中
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["idpzh"] != DBNull.Value && !ds.Tables[0].Rows[0]["idpzh"].ToString().Trim().Equals(""))
                    {
                        try
                        {
                            String pzh = ds.Tables[0].Rows[0]["pzh"].ToString();
                            String idpzh = ds.Tables[0].Rows[0]["idpzh"].ToString();
                            String sql_scrjz = "exec ebksp_wxt_scrjz '" + LoginInfo.getInstance().Hsdwdm + "','" + LoginInfo.getInstance().Dywrq.Year + "','" + dj.Zfid + "','" + pzh + "','" + idpzh + "','" + LoginInfo.getInstance().UserName + "'";
                            DataSet ds_rjz = Dao.getSql(sql_scrjz, "ebksp_wxt_scrjz", out result);
                            if (ds_rjz != null && ds_rjz.Tables.Count > 0 && ds_rjz.Tables[0].Rows.Count > 0 && ds_rjz.Tables[0].Rows[0][0].ToString().Equals("1"))
                            {
                            }
                            else if (ds_rjz != null && ds_rjz.Tables.Count > 0 && ds_rjz.Tables[0].Rows.Count > 0 && ds_rjz.Tables[0].Rows[0][0].ToString().Equals("0"))
                            {
                                MessageBox.Show("生成单据失败，请重试或者联系维护人员！");
                            }
                            else
                            {
                                MessageBox.Show("生成单据失败，错误：" + result);
                            }
                        }
                        catch (Exception e1)
                        {
                            MessageBox.Show("生成单据失败，错误：" + e1.Message);
                        }
                    }
                    //3、如果不存在凭证号则弹出窗口选择凭证号。
                    else
                    {
                        Dialog_cm_SelectPZH dia_pzh = null;
                        try
                        {
                            dia_pzh = new Dialog_cm_SelectPZH();
                            Point p = (sender as Button).PointToScreen(new Point(0, 0));
                            dia_pzh.WindowStartupLocation = WindowStartupLocation.Manual;
                            dia_pzh.Top = p.Y + (sender as Button).Height;
                            dia_pzh.Left = p.X;
                            dia_pzh.ShowDialog();
                        }
                        catch (Exception e1)
                        {
                            MessageBox.Show("获取凭证信息失败，错误信息：" + e1.Message);
                        }
                        if (dia_pzh.DialogResult.Value && !dia_pzh.comb_pzh.Text.Trim().Equals(""))
                        {
                            try
                            {
                                Pzxx pzxx = (dia_pzh.comb_pzh.SelectedItem as Pzxx);
                                String sql_scrjz = "exec ebksp_wxt_scrjz '" + LoginInfo.getInstance().Hsdwdm + "','" + LoginInfo.getInstance().Dywrq.Year + "','" + dj.Zfid + "','" + pzxx.Pzh + "','" + pzxx.Idpzh + "','" + LoginInfo.getInstance().UserName + "'";
                                DataSet ds_rjz = Dao.getSql(sql_scrjz, "ebksp_wxt_scrjz", out result);
                                if (ds_rjz != null && ds_rjz.Tables.Count > 0 && ds_rjz.Tables[0].Rows.Count > 0 && ds_rjz.Tables[0].Rows[0][0].ToString().Equals("1"))
                                {
                                }
                                else if (ds_rjz != null && ds_rjz.Tables.Count > 0 && ds_rjz.Tables[0].Rows.Count > 0 && ds_rjz.Tables[0].Rows[0][0].ToString().Equals("0"))
                                {
                                    MessageBox.Show("生成单据失败，请重试或者联系维护人员！");
                                }
                                else
                                {
                                    MessageBox.Show("生成单据失败，错误：" + result);
                                }
                            }
                            catch (Exception e1)
                            {
                                MessageBox.Show("生成单据失败，错误：" + e1.Message);
                            }
                        }

                    }
                }
                iniData();
            }
        }

        private void btn_queryData_Click(object sender, RoutedEventArgs e)
        {
            iniData();
        }

        private void dg_yhdjxx_all_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }
        //已经支付单据中双击单据
        private void dg_yhdjxx_yzf_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(sender as DataGrid);
            IInputElement obj = (sender as DataGrid).InputHitTest(p);
            //MessageBox.Show(obj.ToString() + ":" + (obj is System.Windows.Controls.Border));
            if (obj != null)
            {
                if ((obj is System.Windows.Controls.TextBlock) || (!(obj is Microsoft.Windows.Controls.DataGridHeaderBorder) && (obj is System.Windows.Controls.Border)))
                {
                    btn_Insert_Click(btn_Insert, null);
                }
            }
        }

        private void tabConTrol_Box_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int tabIndex = (sender as TabControl).SelectedIndex;
            if (tabIndex <= 0)
            {
                this.btn_Cancle.IsEnabled = false;
                this.btn_Insert.IsEnabled = false;
            }
            else if (tabIndex <= 1)
            {
                this.btn_Cancle.IsEnabled = false;
                this.btn_Insert.IsEnabled = false;
            }
            else if (tabIndex <= 2)
            {
                this.btn_Cancle.IsEnabled = false;
                this.btn_Insert.IsEnabled = true;
            }
            else if (tabIndex <= 3)
            {
                this.btn_Cancle.IsEnabled = true;
                this.btn_Insert.IsEnabled = false;
            }
        }
        //搜索条件只过滤当前表格中的过滤信息。直接过滤的时候会影响所有表中的内容展现
        private void btn_search_Click(object sender, RoutedEventArgs e)
        {
            filterData();
        }
        //按照条件过滤数据信息
        private void filterData()
        { 
            DateTime D_DateBegin = this.com_dateStart.D_DateVal;
            DateTime D_DateEnd = this.com_dateEnd.D_DateVal;
            String zy = com_zy.TxtValue;
            String skr = com_zyxx.TxtValue;
            String je = com_je.TxtValue.Trim().Replace(",", "").Replace(" ", "");
            try
            {
                List<Ebk_DJXX> list_all_temp = new List<Ebk_DJXX>();
                List<Ebk_DJXX> list_wzf_temp = new List<Ebk_DJXX>();
                List<Ebk_DJXX> list_yzf_temp = new List<Ebk_DJXX>();
                List<Ebk_DJXX> list_ysc_temp = new List<Ebk_DJXX>();

                foreach (Ebk_DJXX djxx in this.list_djxx_all)
                {
                    //判断摘要
                    if (zy != null && !zy.Equals("") && djxx.Zy.IndexOf(zy) < 0)
                    {
                        continue;
                    }
                    //判断收款人信息
                    if (skr != null && !skr.Equals("") && djxx.Skr.IndexOf(skr) < 0)
                    {
                        continue;
                    }
                    //判断金额
                    if (je != null && !je.Equals(""))
                    {
                        if (Decimal.Parse(je) != djxx.Je)
                        {
                            continue;
                        }
                    }
                    if (djxx.Zfrq != null && !djxx.Zfrq.Equals(DBNull.Value) && !djxx.Zfrq.Trim().Equals(""))
                    {
                        DateTime dt = DateTime.ParseExact(djxx.Zfrq, "yyyy-MM-dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.CurrentInfo);
                        if ((D_DateBegin < dt) && (D_DateEnd > dt))
                        {
                            if (djxx.Zt.Equals("0"))
                            {
                                Ebk_DJXX djxx_wzf = (Ebk_DJXX)djxx.Clone();
                                djxx_wzf.Xh = list_wzf_temp.Count + 1;
                                (list_wzf_temp).Add(djxx_wzf);
                            }
                            if (djxx.Zt.Equals("1"))
                            {
                                Ebk_DJXX djxx_yzf = (Ebk_DJXX)djxx.Clone();
                                djxx_yzf.Xh = list_yzf_temp.Count + 1;
                                (list_yzf_temp).Add(djxx_yzf);
                            }
                            if (djxx.Zt.Equals("2"))
                            {
                                Ebk_DJXX djxx_ysc = (Ebk_DJXX)djxx.Clone();
                                djxx_ysc.Xh = list_ysc_temp.Count + 1;
                                (list_ysc_temp).Add(djxx_ysc);
                            }
                            Ebk_DJXX djxx_all = (Ebk_DJXX)djxx.Clone();
                            djxx_all.Xh = list_all_temp.Count + 1;
                            list_all_temp.Add(djxx_all);
                        }
                    }
                    else
                    {
                        if (djxx.Zt.Equals("0"))
                        {
                            Ebk_DJXX djxx_wzf = (Ebk_DJXX)djxx.Clone();
                            djxx_wzf.Xh = list_wzf_temp.Count + 1;
                            (list_wzf_temp).Add(djxx_wzf);
                        }
                        if (djxx.Zt.Equals("1"))
                        {
                            Ebk_DJXX djxx_yzf = (Ebk_DJXX)djxx.Clone();
                            djxx_yzf.Xh = list_yzf_temp.Count + 1;
                            (list_yzf_temp).Add(djxx_yzf);
                        }
                        if (djxx.Zt.Equals("2"))
                        {
                            Ebk_DJXX djxx_ysc = (Ebk_DJXX)djxx.Clone();
                            djxx_ysc.Xh = list_ysc_temp.Count + 1;
                            (list_ysc_temp).Add(djxx_ysc);
                        }
                        Ebk_DJXX djxx_all = (Ebk_DJXX)djxx.Clone();
                        djxx_all.Xh = list_all_temp.Count + 1;
                        list_all_temp.Add(djxx_all);
                    }
                }
                this.dg_yhdjxx_all.ItemsSource = null;
                this.dg_yhdjxx_wzf.ItemsSource = null;
                this.dg_yhdjxx_yzf.ItemsSource = null;
                this.dg_yhdjxx_ysc.ItemsSource = null;
                this.dg_yhdjxx_all.ItemsSource = list_all_temp;
                this.dg_yhdjxx_wzf.ItemsSource = list_wzf_temp;
                this.dg_yhdjxx_yzf.ItemsSource = list_yzf_temp;
                this.dg_yhdjxx_ysc.ItemsSource = list_ysc_temp;
            }
            catch (Exception e1)
            {
                File.AppendAllText("c:\\log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n" + e1.Message);
            }
        }
        private void com_je_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Return) || e.Key.Equals(Key.Enter))
            {
                filterData();
            }
        }

        private void com_zy_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Return) || e.Key.Equals(Key.Enter))
            {
                filterData();
            }
        }

        private void com_zyxx_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Return) || e.Key.Equals(Key.Enter))
            {
                filterData();
            }
        }

        private void comb_yhzh_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                iniData();
            }
            catch (Exception e1)
            {
                File.AppendAllText("c:\\log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n" + e1.Message);
            }
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            if (this.dg_yhdjxx_ysc.SelectedItems.Count <= 0) return;
            String result = "";
            MessageBoxResult mbr = MessageBox.Show("当前选中"+this.dg_yhdjxx_ysc.SelectedItems.Count+"行数据，即将删除选中行对应的银行收付款单记录，是否继续？", "确认信息", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (mbr.Equals(MessageBoxResult.Yes))
            {
                foreach (Ebk_DJXX dj in this.dg_yhdjxx_ysc.SelectedItems)
                {
                    String sql_qxrjz = "exec ebksp_wxt_qxrjz '" + LoginInfo.getInstance().Hsdwdm + "','" + LoginInfo.getInstance().Dywrq.Year + "','" + dj.Zfid + "','" + LoginInfo.getInstance().UserName + "'";
                    Dao.exeSql(sql_qxrjz, out result);
                }
                iniData();
            }
        }
    }
}
